package at.fhv.dcs.aw.j2me.gps.gpsReader;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/* gpstest.java
 * 
 * Alfred Olschnoegger
 * 17.05.2008
 */

/**
 * TODO description
 *
 * @date 17.05.2008
 * @author Alfred Olschnoegger
 */

public class gpstest extends MIDlet implements CommandListener, ItemStateListener, gpsReader_Observer{

    private final Command m_exitCmd;

    private Form m_form;
    private Alert m_doYouReallyWantToExitAlert;
    public TextField m_State;
    public TextField m_Latitude;
    public TextField m_Longitude;
    public TextField m_Altitude;
    private Alert m_alert;
    
    private gpsReader m_GPSSource;
	
	public gpstest() {
		
        m_exitCmd = new Command("Exit", Command.EXIT, 1);

        m_form = new Form("Just a test");
        Display.getDisplay(this).setCurrent(m_form);

        m_form.addCommand(m_exitCmd);
        m_form.setCommandListener(this);
        m_form.setItemStateListener(this);

        m_State = new TextField("Status", null, 10, TextField.ANY);
        m_Latitude = new TextField("Latitude", null, 10, TextField.ANY);
        m_Longitude = new TextField("Longitude", null, 10, TextField.ANY);
        m_Altitude = new TextField("Altitude", null, 10, TextField.ANY);
        
        m_form.append(m_State);
        m_form.append(m_Latitude);
        m_form.append(m_Longitude);
        m_form.append(m_Altitude);

        m_alert = new Alert("Wrong input");
        m_alert.setTimeout(Alert.FOREVER);
    	m_GPSSource = new gpsReader(this);
    	m_GPSSource.start();
        
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

	protected void pauseApp() {
		// TODO Auto-generated method stub

	}

	protected void startApp() throws MIDletStateChangeException {


	}

    public void commandAction(Command cmd, Displayable d) {

        if (cmd == m_exitCmd) {

            /* Version 2 */
            m_doYouReallyWantToExitAlert = new Alert(null, "Do you really want to exit",null, AlertType.ERROR);
            m_doYouReallyWantToExitAlert.setTimeout(Alert.FOREVER);
            m_doYouReallyWantToExitAlert.setCommandListener(this);
            Display.getDisplay(this).setCurrent(m_doYouReallyWantToExitAlert);
            /* end of version 2 */

        } else if (cmd == Alert.DISMISS_COMMAND && d == m_doYouReallyWantToExitAlert) {
            try {
                destroyApp(true);
            } catch (MIDletStateChangeException e) {
                e.printStackTrace();
            }
            notifyDestroyed();
        }
    }

    public void itemStateChanged(Item item) {
    	// TODO Auto-generated method stub
    	
    }

    public void updateState() {
    	m_State.setString(String.valueOf(m_GPSSource.isAvailable()));
    }
    
	public void updateLocation() {	
		if(m_GPSSource.isAvailable()) {
			m_Latitude.setString(String.valueOf(m_GPSSource.getLatitude()));
			m_Longitude.setString(String.valueOf(m_GPSSource.getLongitude()));
	    	m_Altitude.setString(String.valueOf(m_GPSSource.getAltitude()));
		}
	}

}


// end of gpstest.java
